using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BookInfoCS
{
    [ToolboxData("<{0}:SampleValidator runat=server></{0}:SampleValidator>")]
    public class SampleValidator : System.Web.UI.WebControls.BaseValidator
    {
        protected override bool EvaluateIsValid()
        {
            System.Data.OleDb.OleDbConnection cn = new System.Data.OleDb.OleDbConnection("strConnection");
            System.Data.OleDb.OleDbCommand cmd = cn.CreateCommand();
            string strTestValue;
            string strTestControl;

            try
            {
                this.CheckControlValidationProperty(this.ControlToValidate, "ControlToValidate");
                strTestControl = this.ControlToValidate;
                strTestValue = this.GetControlValidationValue(strTestControl);
                cmd.CommandText = "Select count(*) From Table1 Where " + "KeyValue = '"
                                                                       + strTestValue + "';";
                cn.Open();
                if (Convert.ToInt32(cmd.ExecuteScalar()) == 0)
                {
                    cn.Close();
                    return false;
                }
                else
                {
                    cn.Close();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        protected override bool ControlPropertiesValid()
        {
            System.Web.UI.WebControls.WebControl ctr;

            if (this.ControlToValidate == "")
            {
                throw new System.Web.HttpException("ControlToValidate property not set for " +
                  this.UniqueID);
            }
            else
            {
                if (this.Parent.FindControl(this.ControlToValidate) == null)
                {
                    throw new System.Web.HttpException("Control " + this.ControlToValidate +
                                                                     " found for" + this.ClientID);
                }
                else
                {
                    ctr = (System.Web.UI.WebControls.WebControl)this.Parent.FindControl(this.ControlToValidate);
                    if (ctr.GetType().Name != "TextBox")
                    {
                        throw new System.Web.HttpException("Control " + this.ControlToValidate +
                                                               " not a textbox for " + this.ClientID);
                    }
                }
            }
            return true;
        }

        protected override void Render(HtmlTextWriter writer)
        {
            string strName;
            string strScript;

            if (this.DesignMode == true)
            {
                if (this.DetermineRenderUplevel() && this.EnableClientScript)
                {
                    writer.AddAttribute("controltovalidate", this.ControlToValidate);
                    writer.AddAttribute("evaluationfunction", "MyControlIsValid");
                    writer.AddAttribute("display", this.Display.ToString());
                    writer.AddAttribute("style", "display:none");
                    writer.AddAttribute("errormessage", this.ErrorMessage);

                    this.RegisterValidatorCommonScript();
                    this.RegisterValidatorDeclaration();

                    strName = this.GetControlRenderID(this.ControlToValidate);
                                        
                    strScript = "function MyControlIsValid() {if (document.all['" + strName +
                                                       "'].length > 2){return true} else {return false};}";
                    this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "val" +
                                                       this.ControlToValidate, strScript, true);
                    }
                }
            }
        }
    }


